package com.hsgene.medicine.controller;

import com.hsgene.common.ClientType;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.medicine.condition.GeneticMedicineCondition;
import com.hsgene.medicine.domain.GeneticCancerMedicine;
import com.hsgene.medicine.dto.DaCancerDto;
import com.hsgene.medicine.dto.GeneticCancerMedicineDto;
import com.hsgene.medicine.dto.GeneticMedicineDto;
import com.hsgene.medicine.exception.MedicineErrorCode;
import com.hsgene.medicine.service.MedicineService;
import com.hsgene.medicine.service.api.GeneratorService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc: 癌症药物Controller
 * @author: 江承良
 * @date: 2018/8/15 9:28
 */
@RestController
@RequestMapping("/v1.0/genetic/testing")
public class MedicineController {
    private Logger logger = LogManager.getLogger(MedicineController.class);

    @Autowired
    private MedicineService medicineService;



    /**
     * 获取知识库药物列表
     *
     * @param query  查询条件
     * @param page   当前页
     * @param size 每页显示的数量
     * @return 知识库药物列表信息
     */
    @GetMapping("drugs/knowledge")
    public ApiResult drugPage(String query, Integer page, Integer size) {
        try {
            ApiResult apiResult = medicineService.drugPage(query, page, size);
            logger.info("query drugPage success...");
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("query drugPage failed...", e);
            return ApiResult.fail(MedicineErrorCode.DRUG_PAGE_ERROR);
        }
    }

    /**
     * 添加基因检测药物
     *
     * @param medicineDto 药物信息
     * @return
     */
    @PostMapping("drugs")
    public ApiResult addGeneticMedicine(@RequestBody GeneticMedicineDto medicineDto) {
        try {
            ApiResult apiResult = medicineService.insertGeneticMedicine(medicineDto);
            logger.info("add genetic medicine success...");
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("add genetic medicine failed...", e);
            return ApiResult.fail(MedicineErrorCode.ADD_GENETIC_MEDICINE_ERROR);
        }
    }

    /**
     * 查询列表
     *
     * @param geneticMedicineCondition
     * @return
     */
    @GetMapping("drugs")
    public ApiResult geneticMedicinePage(GeneticMedicineCondition geneticMedicineCondition,
                                         Integer page,Integer size,Integer platform,
                                         @RequestParam(required = false,name = "range_type") String rangeType ) {
        try {
            //如果rangeType为空则查询列表，否则获取全部数据
            if(StringUtils.isEmpty(rangeType)){
                //后台药物列表
                if(ClientType.CONSOLE.getCode().equals(platform)){
                    LimitHelper limitHelper = LimitHelper.create(page, size, true);
                    geneticMedicineCondition.setLimitHelper(limitHelper);
                    ApiResult<Pagination<GeneticMedicineDto>> apiResult = medicineService.geneticMedicinePage(geneticMedicineCondition);
                    logger.info("genetic medicine page success...");
                    return apiResult;
                }else {
                    //花生仁获取药物
                    return medicineService.peanutDrugs();
                }
            }else {
                //获取全部基因检测药物
                ApiResult apiResult = medicineService.geneticMedicineDtos();
                return apiResult;
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("genetic medicine page failed...", e);
            return ApiResult.fail(MedicineErrorCode.GENETIC_MEDICINE_PAGE_ERROR);
        }
    }

    /**
     * 根据id查询基因检测药物
     *
     * @param id 基因检测药物id
     * @return 基因检测药物信息
     */
    @GetMapping("drugs/{id}")
    public ApiResult<GeneticMedicineDto> getGeneticMedicineById(@PathVariable("id") String id) {
        try {
            ApiResult<GeneticMedicineDto> apiResult = medicineService.geneticMedicineById(id);
            logger.info("query genetic medicine success...");
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("query genetic medicine failed...", e);
            return ApiResult.fail(MedicineErrorCode.QUERY_GENETIC_MEDICINE_ERROR);
        }
    }

    /**
     * 更新基因检测药物
     *
     * @param medicineDto 基因检测药物信息
     * @return
     */
    @PutMapping("drugs")
    public ApiResult updateGeneticMedicine(@RequestBody GeneticMedicineDto medicineDto) {
        try {
            ApiResult apiResult = medicineService.updateGeneticMedicine(medicineDto);
            logger.info("update genetic medicine success...");
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("update genetic medicine failed...", e);
            return ApiResult.fail(MedicineErrorCode.UPDATE_GENETIC_MEDICINE_ERROR);
        }
    }

    /**
     * 删除基因检测药物信息
     *
     * @param id 基因检测药物id
     * @return
     */
    @DeleteMapping("drugs/{drug_id}")
    public ApiResult deleteGeneticMedicine(@PathVariable("drug_id") String id) {
        try {
            ApiResult apiResult = medicineService.deleteGeneticMedicine(id);
            logger.info("delete genetic medicine success...");
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("delete genetic medicine failed...", e);
            return ApiResult.fail(MedicineErrorCode.DELETE_GENETIC_MEDICINE_ERROR);
        }
    }

    /**
     * 获取全部基因检测药物数据
     *
     * @return 基因检测药物数据
     */
    @GetMapping("drugs/cancer_medicine/genetic_medicine")
    public ApiResult<List<GeneticMedicineDto>> geneticMedicines() {
        try {
            ApiResult<List<GeneticMedicineDto>> apiResult = medicineService.geneticMedicineDtos();
            logger.info("all genetic medicine success...");
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("all genetic medicine failed...", e);
            return ApiResult.fail(MedicineErrorCode.GENETIC_MEDICINE_PAGE_ERROR);
        }
    }

    /**
     * 添加癌症药物信息
     *
     * @param geneticCancerMedicine
     * @return
     */
    @PostMapping("cancer/drugs")
    public ApiResult insertCancerMedicine(@RequestBody GeneticCancerMedicine geneticCancerMedicine) {
        try {
            if (StringUtils.isEmpty(geneticCancerMedicine.getCancerId())) {
                return new ApiResult(500, "failed", "癌症id不能为空", null);
            }
            ApiResult apiResult = medicineService.insertGeneticCancerMedicine(geneticCancerMedicine);
            logger.info("insert cancer medicine success...");
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("insert cancer medicine failed...", e);
            return ApiResult.fail(MedicineErrorCode.ADD_GENETIC_CANCER_MEDICINE_ERROR);
        }
    }

    /**
     * 癌症药物列表
     *
     * @param geneticMedicineCondition
     * @return
     */
    @GetMapping("cancer/drugs")
    public ApiResult<Pagination<GeneticCancerMedicineDto>> cancerMedicinePage(GeneticMedicineCondition geneticMedicineCondition,
                                                                              Integer page,Integer size) {
        try {
            LimitHelper limitHelper = LimitHelper.create(page, size, true);
            geneticMedicineCondition.setLimitHelper(limitHelper);
            ApiResult<Pagination<GeneticCancerMedicineDto>> apiResult = medicineService.cancerMedicinePage(geneticMedicineCondition);
            logger.info("genetic cancer medicine page success...");
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("genetic cancer medicine page failed...", e);
            return ApiResult.fail(MedicineErrorCode.GENETIC_CANCER_MEDICINE_PAGE_ERROR);
        }
    }

    /**
     * 获取全部癌症药物
     *
     * @param
     * @return
     */
    @GetMapping("drugs/cancer_medicine_all")
    public ApiResult<List<GeneticCancerMedicineDto>> cancerMedicineAll() {
        try {
            ApiResult<List<GeneticCancerMedicineDto>> apiResult = medicineService.cancerMedicineAll();
            logger.info("genetic cancer medicine page success...");
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("genetic cancer medicine page failed...", e);
            return ApiResult.fail(MedicineErrorCode.GENETIC_CANCER_MEDICINE_PAGE_ERROR);
        }
    }

    /**
     * 根据id获取信息
     *
     * @param id
     * @return 癌症药物信息
     */
    @GetMapping("cancer/drugs/{id}")
    public ApiResult<GeneticCancerMedicineDto> cancerMedicineById(@PathVariable("id") String id) {
        try {
            ApiResult<GeneticCancerMedicineDto> apiResult = medicineService.cancerMedicineById(id);
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.fail(MedicineErrorCode.QUERY_GENETIC_CANCER_MEDICINE_ERROR);
        }
    }

    /**
     * 删除癌症药物
     *
     * @param id
     * @return
     */
    @DeleteMapping("cancer/drugs/{id}")
    public ApiResult deleteCancerMedicine(@PathVariable("id") String id) {
        try {
            ApiResult apiResult = medicineService.deleteCancerMedicine(id);
            logger.info("delete cancer medicine success...");
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("delete cancer medicine failed...", e);
            return ApiResult.fail(MedicineErrorCode.DELETE_GENETIC_CANCER_MEDICINE_ERROR);
        }
    }

    /**
     * 更新癌症药物信息
     *
     * @param geneticCancerMedicine
     * @return
     */
    @PutMapping("cancer/drugs")
    public ApiResult updateCancerMedicine(@RequestBody GeneticCancerMedicine geneticCancerMedicine) {
        try {
            ApiResult apiResult = medicineService.updateCancerMedicine(geneticCancerMedicine);
            logger.info("update cancer medicine success...");
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("update cancer medicine failed...", e);
            return ApiResult.fail(MedicineErrorCode.UPDATE_GENETIC_CANCER_MEDICINE_ERROR);
        }
    }

    /**
     * 获取疾病信息
     *
     * @param parentId
     * @return
     */
    @GetMapping("cancer/select")
    public ApiResult<List<DaCancerDto>> cancerInfoByParentId(String parentId) {
        try {
            ApiResult<List<DaCancerDto>> apiResult = medicineService.cancerInfoByParentId(parentId);
            logger.info("get cancerInfo success...");
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("get cancerInfo failed...", e);
            return ApiResult.fail(MedicineErrorCode.QUERY_DA_CANCER_ERROR);
        }
    }

    /***
     * 根据id获取疾病
     * @author  jcl
     * @date 2018/10/26 14:21
     * @param cancerId 疾病id
     * @return
     */
    @GetMapping("cancer/{cancer_id}")
    public ApiResult cancerById(@PathVariable("cancer_id")String cancerId){
        try {
            return medicineService.cancerById(cancerId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("get cancer failed...", e);
            return new ApiResult(500,"failed","获取疾病信息失败",e.toString());
        }
    }
}
